#ifndef NET_TCP_CLIENT_H_
#define NET_TCP_CLIENT_H_

#include "base/base.h"

namespace net {
class TcpStream;
class Poller;
class Buffer;
class Processor;

class TcpClient {
  public:
    TcpClient(Poller* poller, const std::string& ip, uint16 port);
    virtual ~TcpClient();

    void SetReconnectClosure(Closure* closure) {
      ScopedMutex l(&reconnect_mutex_);
      reconnect_closure_ = closure;
    }

    void SetProcessor(Processor* processor);

    bool Start();

  private:
    scoped_ptr<Poller> poller_;
    const std::string ip_;uint16 port_;

    scoped_ptr<Processor> processor_;

    scoped_ptr<TcpStream> stream_;
    scoped_ptr<StopableThread> reconnect_thread_;
    void connect_monitor();
    Mutex reconnect_mutex_;
    SyncEvent reconnect_cond_;
    Closure* reconnect_closure_;
    bool need_reconnect_;
    void NotifyReconnect();

    bool Connect();

    DISALLOW_COPY_AND_ASSIGN(TcpClient);
};
}

#endif
